#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 1, ComboBox
    ui->comboBox_2->addItem("北京");
    ui->comboBox_2->addItem("上海");
    ui->comboBox_2->addItem("深圳");
    ui->comboBox_2->addItem("西安");

    ui->comboBox_2->setCurrentIndex(2);

    // 案例：年-月-日 联动
    for (int i = 1900; i <= 2100; i++)
    {
        ui->yearCombo->addItem(QString::number(i));

    }

    for (int i = 1; i <= 12; i++)
    {
        ui->monthCombo->addItem(QString::number(i));
    }
    this->setDay();

    // 设置当前时间到 dateTimeEdit 中
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());

}

bool Widget::isLeap()
{
    if((this->year % 4 == 0 && this->year % 100 != 0) || this->year % 400 == 0)
    {
        return true;
    }
    return false;
}

void Widget::setDay()
{
    QVector<int> m1 = {1, 3 ,5, 7, 8, 10, 12};
    QVector<int> m2 = {4, 6, 9, 11};
    if(m1.contains(this->month))
    {
        this->day = 31;
    }
    else if (m2.contains(this->month))
    {
        this->day = 30;
    }
    else
    {
        // 判断闰年 平年
        if(this->isLeap())
        {
            this->day = 29;
        }
        else
        {
            this->day = 28;
        }

    }
    ui->dayCombo->clear();
    for (int i = 1; i <= this->day; i++)
    {
        ui->dayCombo->addItem(QString::number(i));
    }
}

void Widget::on_monthCombo_currentIndexChanged(const QString &arg1)
{
    this->month = arg1.toInt();
    this->setDay();
}

void Widget::on_yearCombo_currentIndexChanged(const QString &arg1)
{
    this->year = arg1.toInt();
    this->setDay();
}


Widget::~Widget()
{
    delete ui;
}

// ComboxBox的信号
// actived：当下拉菜单中的选中项发生改变时触发。参数就是选中项的索引号 或者 文本
void Widget::on_comboBox_activated(const QString &arg1)
{
    qDebug() << "on_comboBox_activated(QString) --->" << arg1;
}

void Widget::on_comboBox_activated(int index)
{
    qDebug() << "on_comboBox_activated(int) --->" << index;
}

// currentIndexChanged：当应用程序启动时触发，当下下拉菜单中的选中项发生改变时触发
void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
{
    qDebug() << "on_comboBox_currentIndexChanged(QString)---->" << arg1;
}

void Widget::on_comboBox_currentIndexChanged(int index)
{
    qDebug() << "on_comboBox_currentIndexChanged(int)---->" << index;
}


// currentTextChanged：当文本改变时触发
// ①程序启动时会触发
// ②下拉菜单选中项发生改变时触发
// ③直接输入时触发
void Widget::on_comboBox_currentTextChanged(const QString &arg1)
{
    qDebug() <<"on_comboBox_currentTextChanged(QString)--->" << arg1;
}

void Widget::on_comboBox_highlighted(const QString &arg1)
{
    qDebug() << "on_comboBox_highlighted(QString)" << arg1;
}

// spinBox信号
void Widget::on_spinBox_valueChanged(const QString &arg1)
{
    qDebug() <<"QString" <<  arg1;
}

void Widget::on_spinBox_valueChanged(int arg1)
{
    qDebug() << "int" << arg1;
}

void Widget::on_dial_valueChanged(int value)
{
    qDebug() << value;
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    qDebug() << "slider" << value;
}
